package com.dddd.blog.blogsp.pageprocessor;

import com.dddd.blog.blogsp.entity.Blog;
import lombok.Data;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author 喵星球的大橘
 */
@Component
@Data
public class BlogContentPageProcesser implements PageProcessor {

    private static final String HOME_URL = "https://www\\.cnblogs\\.com/\\w+/$";

    private static final String POST_URL = "https://www\\.cnblogs\\.com/\\w+/p/\\d{8}\\.html";

    private static final String POST_CATEGORY_TAG = "https://www\\.cnblogs\\.com/p/ajax/CategoriesTags.aspx.*";

    private static final String BASE_URL = "https://www.cnblogs.com";

    private static final Pattern patt = Pattern.compile("\\d+");

    private String author;

    private WebDriver driver;

    static{
        System.setProperty("webdriver.chrome.driver","D:\\temp_download\\chromedriver_win32\\chromedriver.exe");
    }

    @Override
    public Site getSite() {
        return Site.me()
                .setRetryTimes(5)
                .setSleepTime(3)
                .setRetrySleepTime(1)
                .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, " +
                        "like Gecko) Chrome/53.0.2785.143 Safari/537.36");
    }

    @Override
    public void process(Page page) {

        Selectable url = page.getUrl();

        if(url.regex(HOME_URL).match()){
            List<String> list =processList(page.getHtml().links().regex(POST_URL).all());
            page.addTargetRequests(list);
        }else if(url.regex(POST_URL).match()){

            Blog blog = new Blog();

            Html html = page.getHtml();

            String title = html.xpath("//*[@id='cb_post_title_url']/span/text()").get();
            String content = html.xpath("//*[@id='cnblogs_post_body']").get();

            //获取时间、阅读数

            String postTime = html.xpath("//*[@id='topics']/div[1]/div[3]/span[1]/text()").get();
            String readCount = html.xpath("//*[@id='topics']/div[1]/div[3]/span[2]/text()").get();
            String commentCount = html.xpath("//*[@id='topics']/div[1]/div[3]/span[3]/text()").get();


            //获取blogId、postId参数
            Matcher matcher = patt.matcher(url.get());
            if(!matcher.find()){
                return;
            }
            String postId = matcher.group();

            String blogStr = page.getHtml().regex("currentBlogId = .*;").get();
            Matcher matcher1 = patt.matcher(blogStr);
            if(!matcher1.find()){
               return;
            }
            String blogId = matcher1.group();

            String target = "https://www.cnblogs.com/"+author+"/ajax/CategoriesTags.aspx?blogId="+blogId+"&postId="+postId;

            if(driver == null){
                ChromeOptions options = new ChromeOptions();
                options.setHeadless(true);
                driver = new ChromeDriver(options);
            }

            driver.get(target);

            List<WebElement> ele_tags = driver.findElements(By.xpath("//*[@id='BlogPostCategory']/a"));

            if(ele_tags.size()==0){
                return;
            }
            List<String> tags = new ArrayList<>();
            for (WebElement ele_tag : ele_tags) {
               tags.add(ele_tag.getText());
            }

            blog.setAuthor(author);
            blog.setUuid(postId);
            blog.setTitle(title);
            blog.setContent(content);


            blog.setCreateTime(processTime(postTime));
            blog.setReadCount(Integer.parseInt(readCount==null ? "0":readCount));
            blog.setCommentCount(Integer.parseInt(commentCount==null? "0":commentCount));

            blog.setTags(processTags(tags));
            blog.setLink(url.get());
            page.putField("blog",blog);
        }
    }


    /**
     * @param list
     * @return
     * 去重链接
     */
    private List<String> processList(List<String> list){

        return list.stream().distinct().collect(Collectors.toList());
    }

    private String processTags(List<String> list){
        StringBuilder builder = new StringBuilder();
        for (String s : list) {
            builder.append(s+",");
        }

        return builder.substring(0,builder.length()-1);
    }

    /**
     * @param timeStr
     * @return
     */
    private Date processTime(String timeStr){

        if(timeStr==null || timeStr == ""){
            return new Date();
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            return format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}


